package com.jiangyg.mall.coupon.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.coupon.dto.CouponSpuCategoryDTO;
import com.jiangyg.mall.coupon.service.CouponSpuCategoryService;
import com.jiangyg.mall.coupon.vo.CouponSpuCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：优惠券分类关联控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-18 21:33:06
 */
@RestController
@RequestMapping("/coupon/spu/category")
public class CouponSpuCategoryController {

    /**
     * 优惠券分类关联接口
     */
    private final CouponSpuCategoryService couponSpuCategoryService;

    @Autowired
    public CouponSpuCategoryController(CouponSpuCategoryService couponSpuCategoryService) {
        this.couponSpuCategoryService = couponSpuCategoryService;
    }

    /**
     * 功能描述：优惠券分类关联分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页优惠券分类关联
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<CouponSpuCategoryVO>> select(HttpServletRequest request, @RequestBody Pagination<CouponSpuCategoryVO> pagination) throws Exception {
        return Result.ok(couponSpuCategoryService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改优惠券分类关联
     *
     * @param request 请求
     * @param dto     优惠券分类关联
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated CouponSpuCategoryDTO dto) throws Exception {
        return Result.saveOrUpdate(couponSpuCategoryService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取优惠券分类关联
     *
     * @param request 请求
     * @param couponSpuCategoryId  优惠券分类关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<CouponSpuCategoryVO> info(HttpServletRequest request, @RequestParam(name = "couponSpuCategoryId") Long couponSpuCategoryId) throws Exception {
        return Result.ok(couponSpuCategoryService.selectById(couponSpuCategoryId));
    }

    /**
     * 功能描述：删除优惠券分类关联
     *
     * @param request 请求
     * @param couponSpuCategoryIds 批量删除的优惠券分类关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> couponSpuCategoryIds) {
        return Result.delete(couponSpuCategoryService.delete(couponSpuCategoryIds));
    }

}
